package com.yang.utils;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 阿里云RocketMQ工具类
 */
@Component
@Slf4j
public class AliRocketMQUtil {

    @Autowired
    private Producer producer;

    /**
     * 发送消息
     * @param topic
     * @param tag
     * @param msg
     * @return
     */
    public SendResult sendMessage(String topic, String tag, String msg) {
        Message message = new Message(topic, tag, msg.getBytes(StandardCharsets.UTF_8));
        message.setKey("data_id");
        SendResult sendResult = producer.send(message);
        return sendResult;
    }

}
